<?php
/**
 * @author 天涯 <mail@phpha.com>
 * @copyright http://blog.phpha.com
 * @link http://blog.phpha.com/vncms
 * @version 1.0
 */
defined('APP_PATH') or exit('Access Denied');
require 'check.php';
class pcat extends check{
	//构造函数
	public function __construct(){
		parent::__construct();
		global $__action;
		$arr = array();
		$pcrow = $__action == 'upd' ? spClass('m_pcat')->findAll("id != ".intval($this->spArgs('id')),'odnum ASC,id ASC') : spClass('m_pcat')->findAll(null,'odnum ASC,id ASC');
		foreach($pcrow as $v){
			$arr[] = array('id'=>$v['id'],'pid'=>$v['pid'],'pcatname'=>$v['pcatname'],'odnum'=>$v['odnum']);
		}
		$treeObj = spClass('Tree',array(array('id','pid','pcatname','format')));
		$this->pcat = $treeObj->getTree($arr);
	}
	//分类列表
	public function lis(){
		$this->display('pcat_lis.tpl.php');
	}
	//添加分类
	public function add(){
		$this->display('pcat_add.tpl.php');
	}
	//执行添加
	public function doadd(){
		if(false != spClass('m_pcat')->spVerifier($this->spArgs()))	$this->error('数据验证错误！',spUrl('pcat','add'));
		if(false != spClass('m_pcat')->create($this->spArgs())){
			$this->success('添加分类成功！',spUrl('pcat','lis'));
		}else{
			$this->error('添加分类失败！',spUrl('pcat','add'));
		}
	}
	//更新排序
	public function updod(){
		$idrow = $this->spArgs('idrow');
		$odnum = $this->spArgs('odnum');
		for($i=0;$i<count($idrow);$i++){
			spClass('m_pcat')->update(array('id'=>$idrow[$i]),array('odnum'=>(int)$odnum[$i]));
		}
		$this->success('更新排序成功！',spUrl('pcat','lis'));
	}
	//更新分类
	public function upd(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('pcat','lis'));
		$this->pcrow = spClass('m_pcat')->find(array('id'=>$id));
		$this->display('pcat_upd.tpl.php');
	}
	//执行更新
	public function doupd(){
		if(false != spClass('m_pcat')->spVerifier($this->spArgs()))	$this->error('数据验证错误！',spUrl('pcat','lis'));
		$pcrow = $this->spArgs();
		if($pcrow['pid'] == 'default')	unset($pcrow['pid']);
		if(false != spClass('m_pcat')->update(array('id'=>$pcrow['id']),$pcrow)){
			$this->success('更新分类成功！',spUrl('pcat','lis'));
		}else{
			$this->error('更新分类失败！',spUrl('pcat','lis'));
		}
	}
	//删除分类
	public function del(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('pcat','lis'));
		if(false != spClass('m_pcat')->find(array('pid'=>$id))){
			$this->error('存在子类禁止删除！',spUrl('pcat','lis'));
		}elseif(false != spClass('m_prod')->find(array('cid'=>$id))){
			$this->error('存在产品禁止删除！',spUrl('pcat','lis'));
		}elseif(false != spClass('m_pcat')->delete(array('id'=>$id))){
			$this->success('删除分类成功！',spUrl('pcat','lis'));
		}else{
			$this->error('删除分类失败！',spUrl('pcat','lis'));
		}
	}
}
